﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.OMM.HealthMonitor.Entity.Router;
using Sinacor.OMM.HealthMonitor.Entity.Receiver;
using Sinacor.OMM.HealthMonitor.Entity.HftProcessor;
using Sinacor.OMM.HealthMonitor.Entity.Dumper;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using Sinacor.OMM.HealthMonitor.Entity.OrderExecutor;
using Sinacor.OMM.HealthMonitor.Entity.Common;
using System.Diagnostics;
using System.IO.Compression;
using System.Xml;

namespace Sinacor.OMM.HealthMonitor.Entity
{
    [Serializable()]
    public class HealthData
    {
        public HealthData()
        {
            _hftProcessorHealthStatus = new SerializableDictionary<string, HftProcessorData>();
        }

        private DateTime _lastUpdated = DateTime.MinValue;
        public DateTime LastUpdated
        {
            get { return _lastUpdated; }
        }

        private ReceiverData _receiverHealthStatus;
        public ReceiverData ReceiverHealthStatus
        {
            get { return _receiverHealthStatus; }
            set
            {
                _lastUpdated = DateTime.Now;
                _receiverHealthStatus = value;
            }
        }


        private RouterData _routerHealthStatus;
        public RouterData RouterHealthStatus
        {
            get { return _routerHealthStatus; }
            set
            {
                _lastUpdated = DateTime.Now;
                _routerHealthStatus = value;
            }
        }

        //private Dictionary<string, HftProcessorData> _hftProcessorHealthStatus;
        //public Dictionary<string, HftProcessorData> HftProcessorHealthStatus
        //{
        //    get { return _hftProcessorHealthStatus; }
        //    set
        //    {
        //        _lastUpdated = DateTime.Now;
        //        _hftProcessorHealthStatus = value;
        //    }
        //}

        private SerializableDictionary<string, HftProcessorData> _hftProcessorHealthStatus;
        public SerializableDictionary<string, HftProcessorData> HftProcessorHealthStatus
        {
            get { return _hftProcessorHealthStatus; }
            set
            {
                _lastUpdated = DateTime.Now;
                _hftProcessorHealthStatus = value;
            }
        }

        private DumperData _dumperHealthStatus;
        public DumperData DumperHealthStatus
        {
            get { return _dumperHealthStatus; }
            set
            {
                _lastUpdated = DateTime.Now;
                _dumperHealthStatus = value;
            }
        }

        private ExecutorData _orderExecutorStatus;

        public ExecutorData OrderExecutorStatus
        {
            get { return _orderExecutorStatus; }
            set
            {
                _lastUpdated = DateTime.Now;
                _orderExecutorStatus = value;
            }
        }

        public static byte[] Serialize(HealthData healthData)
        {
            //byte[] ret;
            //MemoryStream ms = new MemoryStream();
            //BinaryFormatter b = new BinaryFormatter();
            //b.Serialize(ms, healthData);
            //ret = new byte[ms.Length];
            //ms.Position = 0;
            //ms.Read(ret, 0, (int)ms.Length);
            //ms.Flush();
            //ms.Close();
            //ms = null;
            //return ret;

            byte[] ret;
            MemoryStream ms = new MemoryStream();
            XmlSerializer xml = new XmlSerializer(typeof(HealthData));
            xml.Serialize(ms, healthData);

            ret = new byte[ms.Length];
            ms.Position = 0;
            ms.Position = 0;
            ret = Compress(ms);
            ms.Flush();
            ms.Close();
            ms = null;

            

            return ret;
        }

        public static HealthData Deserialize(byte[] serializedData)
        {

            //HealthData ret;
            //MemoryStream ms = new MemoryStream(serializedData);
            //BinaryFormatter b = new BinaryFormatter();
            //ret = (HealthData)b.Deserialize(ms);
            //ms.Flush();
            //ms.Close();
            //ms = null;
            //return ret;

            HealthData ret;
            MemoryStream streamCompressed = new MemoryStream(serializedData);
            streamCompressed.Position = 0;
            MemoryStream ms = new MemoryStream(Decompress(streamCompressed));

            XmlSerializer xml = new XmlSerializer(typeof(HealthData));
            ret = (HealthData)xml.Deserialize(ms);

            ms.Flush();
            ms.Close();
            ms = null;

            return ret;
        }

        private static byte[] Compress(Stream source)
        {
            byte[] buffer;

            using (MemoryStream stream = new MemoryStream())
            {
                using (DeflateStream deflate = new DeflateStream(stream, CompressionMode.Compress, true))
                {
                    source.Copy(deflate);
                    deflate.Close();
                }

                buffer = stream.ToArray();
            }

            return buffer;
        }

        private static byte[] Decompress(Stream source)
        {
            byte[] buffer;

            using (MemoryStream stream = new MemoryStream())
            {
                using (DeflateStream deflate = new DeflateStream(source, CompressionMode.Decompress))
                {
                    deflate.Copy(stream);
                    deflate.Close();
                }

                buffer = stream.ToArray();
            }
            return buffer;
        }

        public void DisposeHftList()
        {
            _hftProcessorHealthStatus = null;
        }
    }
}
